package com.chy.线程.顺序打印0to9;

import java.util.Iterator;

public class MyRunnable implements Runnable {

    private Lock lock;
    public int printNum;

    public MyRunnable(Lock lock, int printNum) {
        this.lock = lock;
        this.printNum = printNum;
    }

    @Override
    public void run() {
        synchronized (lock){
            while (lock.start < lock.end){
                if (printNum == lock.start){
                    System.out.println(printNum);
                    lock.start++;
                    if (lock.start == 10){
                        System.out.println("打印完毕");
                    }
                    lock.notifyAll();
                }else {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
